package api_day10;

import api_day06.Student;
import org.junit.Test;

import java.io.*;
import java.util.Properties;

public class ClassWorkDemo {

    @Test //创建properties配置文件
    public void newPro() throws IOException {
        File p = new File("src/api_day10/pro.properties");
        p.createNewFile();
    }

    @Test //向配置文件中写入信息
    public void writePro() throws IOException {
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("src/api_day10/pro.properties"));
        osw.write("name = 张三");
        osw.write("\nage = 18");
        osw.close();
    }
    @Test //读取配置文件信息并封装到学生对象中，然后输出到本地文件
    public void readPro() throws IOException {
        //获取配置文件路径
        String path = ClassWorkDemo.class.getResource("pro.properties").getPath();
        //创建输入流对象
        InputStreamReader isr = new InputStreamReader(new FileInputStream(path));
        //创建properties类对象
        Properties p = new Properties();
        //加载配置文件信息
        p.load(isr);
        //获取配置文件信息
        String name = p.getProperty("name");
        String age = p.getProperty("age");
        //将获取到的属性封装成Student对象
        Student stu = new Student(name,Integer.parseInt(age));
        System.out.println(stu);
        //将对象通过对象流输出到本地文件
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("src/api_day10/stu.obj"));
        oos.writeObject(stu);
        oos.close();
    }
}